/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.proxy11;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.task.SpiderTaskQueue;
import lombok.Data;
import org.shoulder.core.util.ConvertUtil;
import org.shoulder.core.util.JsonUtils;
import org.springframework.util.CollectionUtils;

import java.net.Proxy;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * https://proxy11.com/free-proxy 可以直接请求接口，根据参数组合遍历
 * https://proxy11.com/api/demoweb/proxy.json    ?type=xxx&google=xxx&country=hk&speed=2000&port=8080
 * </p>
 * <p>
 * 【优化】组装参数，爬更多地区
 * 2023.4 无法跳过html请求，直接访问json接口了
 *
 * @author lym
 */
//@Component
public class Proxy11ResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    public Proxy11ResultHandler() {
        super("proxy11");
    }

    @Override
    public void startSpiders() {
        // 只爬 cn 的
        //IpProxySpiderTask task = createTask("https://proxy11.com/api/demoweb/proxy.json?country=cn");

        SpiderTaskQueue.putTask(createTask("https://proxy11.com/free-speed"));
        SpiderTaskQueue.putTask(createTask("https://proxy11.com/free-proxy"));

        // 所有区域的
//        for (RegionEnum regionEnum : RegionEnum.values()) {
//            IpProxySpiderTask regionFilterTask = createTask("https://proxy11.com/api/demoweb/proxy.json?country=" + regionEnum.getAlpha2Code());
//            SpiderTaskQueue.putTask(regionFilterTask);
//        }
    }


    /**
     * 每 5 分钟触发一次
     */
    @Override
    public String executeCron() {
        return "0 0/10 * * * ?";
    }

    /**
     * 处理响应
     */
    @Override
    public void doHandle(IpProxySpiderTask task, String html) {

        List<String> contentMatchResult = ReUtil.findAllGroup1("<tbody>(.*?)</tbody>", html);
        if (CollectionUtils.isEmpty(contentMatchResult)) {
            log.warn("illegal match result " + task.getUrl());
            return;
        }
        String content = contentMatchResult.get(0);
//        <tr>
//                        <td>23.227.38.254</td>
//                        <td>80</td>
//                        <td>Transparent</td>
//                        <td><span class="fi fi-ca"></span> Canada</td>
//                        <td>0.29 </td>
//                        <td> <script async>converter1(1680334545.0)</script></td>
//                    </tr>
        String[] lines = content.split("</tr>");
        for (int i = 0; i < lines.length - 1; i++) {
            String[] atrArr = lines[i].split("</td>");
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source("proxy11")
                    .ip(atrArr[0].substring(atrArr[0].lastIndexOf('>')))
                    .port(Integer.parseInt(atrArr[1].substring(atrArr[1].lastIndexOf('>')).trim()))
                    .anonymity(atrArr[2].substring(atrArr[2].lastIndexOf('>')).trim())
                    .location(atrArr[3].substring(atrArr[3].lastIndexOf('>')).trim())
                    .responseTime(Long.parseLong(atrArr[1].substring(atrArr[1].lastIndexOf('>')).trim()) * 1000)
                    .build();
            addProxyToSourceRepository(proxyInfo);
        }

        //parseFromJson(html);
    }

    @Deprecated//("不能跳过html了")
    private void parseFromJson(String json) {
        DataWrapper wrapper = JsonUtils.parseObject(json, DataWrapper.class);
        List<Proxy11DTO> data = wrapper.getData();

        data.stream().map(Proxy11DTO::toStandardModel).forEach(this::addProxyToSourceRepository);
    }

    @Data
    public static class DataWrapper {
        List<Proxy11DTO> data;
    }

    @Data
    public static class Proxy11DTO {

        private String ip;

        private String port;

        private String time;

        /**
         * 匿名类型 0 透明  1 匿名
         */
        private int type;

        private String country;

        private String country_code;

        /**
         * 可以访问 google、Instagram   0 不可以  1 可以
         */
        private int google;

        /**
         * Fri, 31 Dec 2021 08:02:47 GMT
         */
        private Date createAt;

        private Date updateAd;

        public ProxyInfo toStandardModel() {
            return ProxyInfo.builder()
                    .source("proxy11")
                    .ip(ip)
                    .port(Integer.parseInt(port))
                    .anonymity(type == 1 ? "匿名" : "透明")
                    .type(Proxy.Type.HTTP)
                    .location(country)
                    .responseTime(ProxyInfo.parseSecondToMill(time))
                    .createTime(ConvertUtil.convert(createAt, LocalDateTime.class))
                    .lastVerificationTime(ConvertUtil.convert(updateAd, LocalDateTime.class))
                    .build();
        }

    }
}